package com.zxb.user.test;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;

import java.util.Properties;
import java.util.concurrent.Executor;

public class ConfigServerDemo {

    public static void main(String[] args) throws NacosException, InterruptedException {
        String serverAdd = "127.0.0.1:8848";
        String dataId = "nacos-config-dome.yaml";
        String group = "DEFAULT_GROUP";
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR,serverAdd);

        //获取配置中心服务
        ConfigService configService = NacosFactory.createConfigService(properties);

        //从配置中心拉取配置
        String config = configService.getConfig(dataId, group, 5000);
        System.out.println(config);
        //注册监听
        configService.addListener(dataId, group, new Listener() {
            @Override
            public Executor getExecutor() {
                return null;
            }

            @Override
            public void receiveConfigInfo(String configInfo) {
                System.out.println("感知配置变化:" + configInfo);
            }
        });

        //发送properties格式
        boolean b = configService.publishConfig(dataId, group, "common.age = 18", ConfigType.PROPERTIES.getType());
        System.out.println(b);

        Thread.sleep(Integer.MAX_VALUE);
    }

}
